package com.hl.card.credit.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.hl.card.credit.controller.CreditCardOrderController;
import com.hl.card.credit.domain.CardStatus;
import com.hl.card.credit.domain.CreditCardOrder;
import com.hl.card.credit.domain.InitailFormDTO;
import com.hl.card.credit.service.CreditCardOrderService;
import com.hl.card.credit.service.PingAnCreditCardService;
import com.hl.card.credit.utill.HttpClientTool;
import com.hl.card.credit.utill.PingAnTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import static com.hl.card.credit.utill.StatusTool.getSign4;


@Service
public class PingAnCreditCardServiceImpl implements PingAnCreditCardService {


	@Autowired
	private CreditCardOrderService creditCardOrderService;

    @Value("${spring.profiles.active}")
	private String active;
    @Value("${curl}")
    private String curl;
    @Value("${merchantKey}")
    private String merchantKey;
    private static final Logger logger = LoggerFactory.getLogger(CreditCardOrderController.class);
    @Override
    public String getRegUrl(String seqId) {
        String url="";
        try {
            InitailFormDTO initDTO=new InitailFormDTO();
            /***************************参数赋值，请和业务确定*********************************************/
            initDTO.setPartnerSeqId(seqId);
            initDTO.setScc("235180089");
            initDTO.setMt("123456789");
            initDTO.setCcp("1a2a3a9");
            initDTO.setVersionNo("R10310");
            initDTO.setOnlineSQFlag("N");
            initDTO.setCardCatenaNo("01a02a04");
            initDTO.setChannel("WXHZF");
            /***************************中文记得转码UTF-8*************************************************/
            System.out.println("需要加密的入参:"+initDTO);
            if(active.equals("dev")){
                url= PingAnTool.getUrl(initDTO);
            }else{
                url= PingAnTool.getUrlReal(initDTO);
            }
            System.out.println( "最终生成测试环境url：" + url);
        }catch (Exception e){
            logger.error("请求平安地址出错"+e.getMessage());
        }
        return url;
    }
    @Override
    public CreditCardOrder getSatusByOrderId(String orderId) {
    	CreditCardOrder order=	creditCardOrderService.findByOrderId(orderId);
        return order;
    }
    @Override
    public CardStatus getPortStatus(String orderId) {
//        String curl = "https://c.pingan.com/ca/scc/merchant/order/getStatus";
        Map<String,String[]> map = new TreeMap();
        map.put("merchant_key",new String[]{merchantKey});
        map.put("order_id", new String[]{orderId});
//        map.put("timestamp",new String[]{"1520305365933"});
        map.put("timestamp",new String[]{System.currentTimeMillis()+""});
        String sign=getSign4(map);
        System.out.println(sign);
        Map param=new HashMap();
        param.put("order_id",map.get("order_id")[0]);
        param.put("timestamp",map.get("timestamp")[0]);
        param.put("sign",sign);
        HttpClientTool.createSSLClientDefault();
        String res=HttpClientTool.doGetSSL(curl,param);
        JSONObject object=JSON.parseObject(res);
        JSONObject obj= (JSONObject) object.get("data");
        CardStatus cardStatus= JSON.toJavaObject(obj,CardStatus.class);
        return cardStatus;
    }
}
